//
//  YLoginViewController.swift
//  rxCoreLogic
//
//  Created by yq on 2021/1/27.
//

import UIKit
import RxSwift
import RxCocoa

class YLoginViewController: YViewController {
	
	@IBOutlet weak var userNameOutlet: UITextField!
	@IBOutlet weak var userNameValidOutlet: UILabel!
	@IBOutlet weak var passwordOutlet: UITextField!
	@IBOutlet weak var passwordValidOutlet: UILabel!
	@IBOutlet weak var bottomButton: UIButton!
	
	override func viewDidLoad() {
        super.viewDidLoad()

		setupUI()
		binding()
    }
	
}

//MARK: - ✨== binding ==✨
extension YLoginViewController {

	func setupUI() {
		bottomButton.layer.cornerRadius = 5
		bottomButton.layer.masksToBounds = true
	}
	
	func binding() {

		let minimalUsernameLength = 5
		let minimalPasswordLength = 5

		let usernameValid = userNameOutlet.rx.text.orEmpty
			.map({ $0.count >= minimalUsernameLength })
			.share(replay: 1)
		
		usernameValid.bind(to: passwordOutlet.rx.isEnabled)
			.disposed(by: disposeBag)

		let passwordValid = passwordOutlet.rx.text.orEmpty
			.map({ $0.count >= minimalPasswordLength })
			.share(replay: 1)
		

// 方式一: - 控制描述标签隐藏
//		usernameValid.bind(to: userNameValidOutlet.rx.isHidden)
//			.disposed(by: disposeBag)
//		passwordValid.bind(to: passwordValidOutlet.rx.isHidden)
//			.disposed(by: disposeBag)
		
// 方式二: - 控制底部按钮可点击
//		let everyThingValid = Observable.combineLatest(usernameValid, passwordValid){ $0 && $1 }
//			.share(replay: 1)
//		everyThingValid.bind(to: bottomButton.rx.isEnabled)
//			.disposed(by: disposeBag)
		
		// 方式三: - 动态显示标签提示语
		usernameValid.asObservable()
			.skip(1)
			.subscribe(onNext: {[weak self] valid in
				guard let self = self else { return }
				self.userNameValidOutlet.textColor = valid ? UIColor.green : UIColor.red
				self.userNameValidOutlet.text = valid ?  "用户名有效" : "用户名至少 5 个字符"
			})
			.disposed(by: disposeBag)
		

		passwordValid.asObservable()
			.skip(1)
			.subscribe(onNext: { [weak self] valid in
				guard let self = self else { return }
				self.passwordValidOutlet.textColor = valid ?  UIColor.green : UIColor.red
				self.passwordValidOutlet.text =  valid ?  "密码有效" : "密码至少 5 个字符"
			})
			.disposed(by: disposeBag)
		
		
		let params = Observable.combineLatest(usernameValid, passwordValid)
			.map({(usernameValid: $0.0, passwordValid: $0.1)})
			.share(replay: 1)
				
		bottomButton.rx.tap
			.withLatestFrom(params)
			.flatMapLatest { (args) -> Observable<Void> in

				guard args.usernameValid else {
					self.showHud(string: "用户名格式不对")
					return .empty()
				}
				guard args.passwordValid else {
					self.showHud(string: "密码格式不对")
					return .empty()
				}

				self.showHud(string: "good")

				return .empty() // 这里调用接口,获取点击按钮请求的接口
			}
			.subscribe(onNext: {[weak self] in
			guard let self = self else { return }
			let alertV = UIAlertController(title: "提示", message: "底部按钮点击", preferredStyle: .alert)
			let cancel = UIAlertAction(title: "取消", style: .destructive) {[weak self] (_) in
				self!.dismiss(animated: true, completion: nil)
			}
			let sure = UIAlertAction(title: "确定", style: .default) { [weak self] (_) in
				self!.dismiss(animated: true) {
					print("点击确定")
				}
			}
			alertV.addAction(cancel)
			alertV.addAction(sure)
			self.present(alertV, animated: true, completion: nil)
		}).disposed(by: disposeBag)
	}
	
}
